package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;


    //扩展方法，保存菜品，菜品口味数据
    @Override
    @Transactional
    public void saveDisWithFlavor(DishDto disDto) {
        //1.添加菜品信息到dish一条数据
        this.save(disDto);

        //1.添加菜品口味信息到dish——flavor多条数据

        List<DishFlavor> flavors = disDto.getFlavors();
        //TODO 给每个口味设置菜品id
        flavors.forEach(flavor -> flavor.setDishId(disDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }


    //分夜查询菜品名称
    @Override
    public Page<DishDto> pageDishDto(Long page, Long pageSize, String name) {
        //查两张表（dish,category），还得分页
        //左外查询select * from dish  left (outer) join category on

        //创建分页对象
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        //调用mapper
        return dishMapper.pageDishDto(pageInfo,name);

    }



    //扩展方法：批量删除菜品，以及菜品口味数据，如果有菜品状态为启售，则不做操作
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //1. 根据菜品ids，查询其中是否有菜品为启售
        // select count(*) from dish where id in (ids) and status=1
        Integer count = this.lambdaQuery()
            .in(Dish::getId, ids)
            .eq(Dish::getStatus, 1)
            .count();
        if (count > 0) {
            throw new CustomerException("有菜品正在售卖，不能删除！");
        }

        //2. 删除菜品信息
        // delete from dish where id in (ids)
        this.removeByIds(Arrays.asList(ids));

        //3. 删除菜品口味信息
        // delete from dish_flavor where dish_id in (ids)
        dishFlavorService.lambdaUpdate()
            .in(DishFlavor::getDishId, ids)
            .remove();
    }

    //扩展方法，根据分类查询菜品包含口味信息
    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        List<DishDto>  dishDtoList = dishMapper.listDishDto(categoryId,name);
        return dishDtoList;
    }

    @Override
    public List<DishDto> getDishBySetmealId(Long setmealId) {
        return dishMapper.getDishBySetmealId(setmealId);
    }

    //扩展方法：根据id查询菜品详情信息（包含口味数据）
    @Override
    public DishDto getDishByIdWithFlavor(Long id) {
        //select * from dish d left join dish_flavor df on d.id = df.dish_id where d.id = ?
        DishDto dishDto = dishMapper.getDishByIdWithFlavor(id);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishWithFlavor(DishDto dishDto) {
        //1. 修改菜品dish表数据
        this.updateById(dishDto);

        //2. 修改菜品口味数据（先删再加）
        // delete from dish_flavor where dish_id=?
        dishFlavorService.lambdaUpdate()
            .eq(DishFlavor::getDishId, dishDto.getId())
            .remove();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //TODO 给每个口味设置菜品id
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //批量保存
        dishFlavorService.saveBatch(flavors);
    }
}






